<?php

defined('SYSPATH') OR die('No direct access allowed.');

class Controller_Photo extends Controller_Initialize {

  public $secure_actions = array(
      // user actions
      'index' => 'login',
  );

  public function action_index() {

    $view = View::factory('templates/photo/uploaded')
      ->bind('photo_user', $result)
      ->bind('pagination', $pagination)
      ->bind('sort', $sort);

    $photo_user = ORM::factory('photo_user')->where('username', '=', $this->userAuth->username);

    $total_items = $photo_user->count_all();

    if (! isset($_GET['sort'])) {
      $sort = 'date';
      $dir = 'DESC';
    } elseif ($_GET['sort'] == 'new') {
      $sort = 'date';
      $dir = 'DESC';
    } elseif ($_GET['sort'] == 'old') {
      $sort = 'date';
      $dir = 'ASC';
    } elseif ($_GET['sort'] == 'prf') {
      $sort = 'date';
      $dir = 'DESC';      
      $total_items = $photo_user->where('user_profile_only', '=', '1')->count_all();
      $photo_user->where('user_profile_only', '=', '1');
    }

    $pagination = new Pagination(array(
      'total_items' => $total_items,
      'items_per_page'=> 6,
      'auto_hide' => true,
    ));

    $result = $photo_user->limit($pagination->items_per_page)->offset($pagination->offset)->order_by($sort, $dir)
              ->where('username', '=', $this->userAuth->username)->find_all();

    $this->template->content = $view;

  }

  public function action_view($id) {

    $this->template->content = View::factory('templates/photo/view')
      ->bind('photo_user', $photo_user)
      ->bind('comments_photo', $comments_photo);

    $photo_user = ORM::factory('photo_user')->where('id', '=', $id)->find();
    $comments_photo = ORM::factory('comments_photo')->where('photo_user_id', '=', $id)->find_all();

  }

  public function action_edit($id) {

    if (Auth::instance()->logged_in('admin')) {
      $model = ORM::factory('photo_user')->find($id);
    } else {
      $model = ORM::factory('photo_user')->where('username', '=', $this->userAuth->username)->find($id);
    }

    if (!empty($_POST)) {
      $post = $model->validate_upload_inputs($_POST);

      if ($post->check()) {
        echo kohana::debug($post);
        $model->title = $post['title'];
        $model->description = $_POST['description'];
        $model->save();

        foreach ($model->photo_tag->find_all() as $removed) {
          $model->remove('photo_tag', $removed);
        }

        foreach ($_POST['tags'] as $item) {
          $photo_tag = ORM::factory('photo_tag')->where('tag', '=', $item)->find();

          if ($photo_tag->id) {
            $model->add('photo_tag', $photo_tag);
          } else {
            $photo_tag_new = ORM::factory('photo_tag');
            $photo_tag_new->tag = $item;
            $photo_tag_new->save();
            $model->add('photo_tag', $photo_tag_new);
            $photo_tag_new->clear();
          }
        }

        Message::add('success', '<strong>Změny byly uloženy.</strong>');

      } else {
        $informations = null;
        foreach ($post->errors('validate') as $item) {
          $informations .= '<span>' . $item . '</span>';
        }

        $this->template->infoPanelTop->errors = $post->errors('validate');
        Message::add('warning', '<strong>Změny nebyly uloženy.</strong> ' . $informations);
      }
      Request::instance()->redirect('photo/view/'.$id);
            
    } else {

      $this->template->content = View::factory('templates/photo/edit')
                      ->bind('photo_user', $model);
    }
  }


  public function action_delete($id) {

    if (Auth::instance()->logged_in('admin')) {
      $model = ORM::factory('photo_user')->find($id);
    } else {
      $model = ORM::factory('photo_user')->where('username', '=', $this->userAuth->username)->find($id);    
    }
    unlink('files/users/'.$model->username.'/'.$model->thumb);
    $model_table= $model->photo_source->find_all();
    if (!empty($model_table))
    foreach ($model_table as $item) {
      unlink('files/users/'.$model->username.'/'.$item->filename);
      if (file_exists('files/users/'.$model->username.'/m_'.$item->filename)) {
        unlink('files/users/'.$model->username.'/m_'.$item->filename);
      }
      if (file_exists('files/users/'.$model->username.'/o_'.$item->filename)) {
        unlink('files/users/'.$model->username.'/o_'.$item->filename);
      }
    }

    if (Auth::instance()->logged_in('admin')) {
      if (ORM::factory('photo_user')->delete($id)) {
        Message::add('success', 'Fotografie byla úspěšně smazána.');
        Request::instance()->redirect('administration/gallery');
      }
    } else {
      if (ORM::factory('photo_user')->where('username', '=', $this->userAuth->username)->delete($id)) {
        Message::add('success', 'Fotografie byla úspěšně smazána.');
      }
    }

    Request::instance()->redirect('/');

  }

}